package com.school.mall.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.mall.common.result.R;
import com.school.mall.sys.entity.MallGoodsColour;
import com.school.mall.sys.entity.MallGoodsSize;
import com.school.mall.sys.entity.queryVo.MallGoodsColourQueryVo;
import com.school.mall.sys.entity.queryVo.MallGoodsSizeQueryVo;
import com.school.mall.sys.service.MallGoodsColourService;
import com.school.mall.sys.service.MallGoodsSizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品类型表 前端控制器
 */
@RestController
@RequestMapping("/sys/mallGoodsSize")
@Api(tags="商品尺寸表")
public class MallGoodsSizeController {

    @Autowired
    MallGoodsSizeService iMallGoodsSizeService;

        /**
        * 查询商品尺寸表列表
        */
        @PostMapping("queryMallGoodsSizeList")
        @ApiOperation("商品尺寸表列表")
        public R queryMallGoodsSizeList(@RequestBody MallGoodsSizeQueryVo queryVo) {

            QueryWrapper<MallGoodsSize> queryWrapper=new QueryWrapper<>();
            Page<MallGoodsSize> page = iMallGoodsSizeService.page(queryVo.getPage(), queryWrapper);
            return R.ok().data("dataList",page);
        }

        /**
         * 保存或者更新商品尺寸表信息
         */
        @PostMapping("saveOrUpdateMallGoodsSize")
        @ApiOperation("保存或者更新商品尺寸表信息")
        public R saveOrUpdateMallGoodsSize(@RequestBody MallGoodsSize entity) {
            boolean b =true;
            if(StringUtils.isEmpty(entity.getId())){
                b= iMallGoodsSizeService.save(entity);
            }else{
                b=iMallGoodsSizeService.updateById(entity);
            }
            return R.th(b);
        }


        /**
         * 根据id查询商品尺寸表信息
         */
        @GetMapping("getByIdMallGoodsSize/{id}")
        @ApiOperation("根据id查询商品尺寸表信息")
        public R getByIdMallGoodsSize(@PathVariable("id") String id) {
             MallGoodsSize dataMallGoodsSize=  iMallGoodsSizeService.getById(id);
             return R.ok().data("entity",dataMallGoodsSize);

        }
        /**
         * 根据id删除商品尺寸表信息
         */
        @DeleteMapping("removeByIdMallGoodsSize/{id}")
        @ApiOperation("根据id删除商品尺寸表信息")
        public R removeByIdMallGoodsSize(@PathVariable("id") String id) {
            boolean effect= iMallGoodsSizeService.removeById(id);
            return R.th(effect);

        }
        @DeleteMapping("dropByIdsMallGoodsSize/{ids}")
        @ApiOperation("根据id数组多选删除商品尺寸表信息")
        public R dropByIdsMallGoodsSize(@PathVariable("ids") String ids){
            List<String> idList = Arrays.asList(ids.split(","));
            boolean b = iMallGoodsSizeService.deleteBatchIds(idList);
            return R.th(b);
        }

        @GetMapping("mallGoodsSizeList")
        public R mallGoodsSizeList(){
            List<MallGoodsSize> list = iMallGoodsSizeService.list();
            List<Map<String, String>> collect = list.stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());

            return R.ok().data("dataList",collect);
        }

}

